package com.hzc.gulimall.seckill.scheduled;

import com.hzc.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 每天晚上三点上架最近三天需要秒杀的商品
 * 00:00:00 - 23:59:59
 */

@Slf4j
@Service
public class SeckillSkuScheduled {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    RedissonClient redissonClient;

    private final String UPLOAD_LOCK = "scekill:upload:lock";

    // @Scheduled(cron = "0 0 3 * * ?")
    @Scheduled(cron = "0/20 * * * * ?")
    public void uploadSeckillSkuLatest3Day() {
        //重复上架无需处理
        RLock lock = redissonClient.getLock(UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSkuLatest3Day();
        } finally {
            lock.unlock();
        }
    }
}
